package com.znsb.msfq.album;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.znsb.msfq.R;
import com.znsb.msfq.utils.DisplayUtil;
import com.znsb.msfq.utils.ImageLoader;
import com.znsb.msfq.view.selectorphoto.Bimp;

import java.io.File;

@SuppressLint("HandlerLeak")
public class PublishGridAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    private Context context;

    public PublishGridAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    public int getCount() {
        if (Bimp.tempSelectBitmap.size() == 3) {
            return 3;

        }
        return (Bimp.tempSelectBitmap.size() + 1);
    }

    public void updateList() {
        notifyDataSetChanged();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.plugin_published_grida, parent, false);
            holder = new ViewHolder();
            holder.image = (ImageView) convertView.findViewById(R.id.item_grida_image);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        ViewGroup.LayoutParams p = holder.image.getLayoutParams();
        p.width = DisplayUtil.dip2px(100);
        p.height = DisplayUtil.dip2px(100);
        holder.image.setLayoutParams(p);
        holder.image.setScaleType(ImageView.ScaleType.FIT_XY);
        if (position == Bimp.tempSelectBitmap.size()) {
            holder.image.setImageBitmap(BitmapFactory.decodeResource(context.getResources(), R.mipmap.addcomments_imges));
            holder.image.setScaleType(ImageView.ScaleType.CENTER);
        } else {
//            holder.image.setImageBitmap(Bimp.tempSelectBitmap.get(position).getBitmap());
            File file = new File(Bimp.tempSelectBitmap.get(position).getImagePath());
            ImageLoader.imageLoder(holder.image, file);
        }
        return convertView;
    }
    public class ViewHolder {
        public ImageView image;
    }

}
